package cn.wolfcode.wolf2w.business.listen;

import cn.wolfcode.wolf2w.api.domain.Answer;
import cn.wolfcode.wolf2w.api.domain.AnswerAttention;
import cn.wolfcode.wolf2w.api.domain.Question;
import cn.wolfcode.wolf2w.api.domain.QuestionAttention;
import cn.wolfcode.wolf2w.business.mapper.AnswerAttentionMapper;
import cn.wolfcode.wolf2w.business.mapper.QuestionAttentionMapper;
import cn.wolfcode.wolf2w.business.service.IAnswerService;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class QuestionListener implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private IQuestionService questionService;
    @Autowired
    private QuestionAttentionMapper questionAttentionMapper;
    @Autowired
    private AnswerAttentionMapper answerAttentionMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IAnswerService answerService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 清除缓存
        Collection<String> questionKeys = redisService.keys("qa_*");
        Collection<String> answerKeys = redisService.keys("aa_*");
        redisService.deleteObject(questionKeys);
        redisService.deleteObject(answerKeys);
        // 根据需求查询近期数据
        List<Question> questionList = questionService.list();
        for (Question question : questionList) {
            String key = RedisKeys.QUESTION_STATUS_HASH.join(question.getId().toString());
            if (!redisService.hasKey(key)) {
                Map<String, Object> map = new HashMap<String, Object>() {{
                    this.put("viewNum", question.getViewNum());
                    this.put("count", question.getCount());
                    this.put("zanCount", question.getZanCount());
                    this.put("id", question.getId());
                }};
                redisService.setCacheMap(key, map);
            }
        }
        List<Answer> answerList = answerService.list();
        for (Answer answer : answerList) {
            String key = RedisKeys.ANSWER_STATUS_HASH.join(answer.getId().toString());
            if (!redisService.hasKey(key)) {
                Map<String, Object> map = new HashMap<String, Object>() {{
                    this.put("zanCount", answer.getZanCount());
                    this.put("id", answer.getId());
                }};
                redisService.setCacheMap(key, map);
            }
        }
        // 获取问题收藏数据
        QueryWrapper<QuestionAttention> questionAttentionQueryWrapper = new QueryWrapper<>();
        questionAttentionQueryWrapper.select("DISTINCT id");
        questionAttentionMapper.selectList(questionAttentionQueryWrapper).forEach(qa -> {
            LambdaQueryWrapper<QuestionAttention> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(QuestionAttention::getUserId);
            Set<Long> userIds = questionAttentionMapper.selectList(wrapper).stream().map(QuestionAttention::getUserId).collect(Collectors.toSet());
            redisService.setCacheSet(RedisKeys.QUESTION_FAVOR.join(qa.getId().toString()), userIds);
        });
        // 获取答案收藏数据
        QueryWrapper<AnswerAttention> answerAttentionQueryWrapper = new QueryWrapper<>();
        answerAttentionQueryWrapper.select("DISTINCT id");
        answerAttentionMapper.selectList(answerAttentionQueryWrapper).forEach(qa -> {
            LambdaQueryWrapper<AnswerAttention> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(AnswerAttention::getUserId);
            Set<Long> userIds = answerAttentionMapper.selectList(wrapper).stream().map(AnswerAttention::getUserId).collect(Collectors.toSet());
            redisService.setCacheSet(RedisKeys.ANSWER_FAVOR.join(qa.getId().toString()), userIds);
        });
        log.info("问答浏览关注数据初始化成功");
    }
}
